﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FileHelper;

public partial class Folders_ctrfoldermanager : System.Web.UI.UserControl
{
    string urladdfolder = "addfolder.aspx";
    string urlAddFile = "/Files/addfile.aspx";

    public string BaseFileServerUrl = "http://localhost:8080/";

    public string User
    {
        get
        {
            return string.Empty;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            hdCurrentFolderId.Value = "-1";
            this.BuildFolders();
            this.BuildFiles();
        }
    }

    private void BuildFolders()
    {
        int currentId = Convert.ToInt32(hdCurrentFolderId.Value);        
        List<FolderInfo> lst = FileStreamManager.GetFolders(currentId, User, 0, true, int.MaxValue, 1);
        this.rptFolders.DataSource = lst;
        this.rptFolders.DataBind();
        
    }

    private void BuildFiles()
    {
        int currentId = Convert.ToInt32(hdCurrentFolderId.Value);       
        List<FileInfo> lst = FileStreamManager.GetFiles(currentId, 0, true, int.MaxValue, 1);
        this.rptFiles.DataSource = lst;
        this.rptFiles.DataBind();
    }      

    protected void ltnNewFolder_Click(object sender, EventArgs e)
    {
        Response.Redirect(urladdfolder + "?parentid="+hdCurrentFolderId.Value+"&folderid=-1&owner="+User);
    }

    protected void lbtnBack_Click(object sender, EventArgs e)
    {
        //StreamingServiceClient proxy = new StreamingServiceClient();
        int currentId = Convert.ToInt32(hdCurrentFolderId.Value);
        if (currentId > 0)
        {
            FolderInfo objCurrentFolder = FileStreamManager.GetFolder(currentId);

            this.hdCurrentFolderId.Value = objCurrentFolder.ParentId.ToString();
        }                
        
        this.BuildFolders();
        this.BuildFiles();
        
    }

    protected void rptFolders_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToUpper() == "CMDLIST")
        {
            int selecteId = Convert.ToInt32(e.CommandArgument);
            this.hdCurrentFolderId.Value = selecteId.ToString();                                              
            this.BuildFolders();
            this.BuildFiles();           
        }
    }

    protected void lbtnNewFile_Click(object sender, EventArgs e)
    {
        Response.Redirect(urlAddFile + "?folderid=" + hdCurrentFolderId.Value + "&owner=" + User);
    }

    public string GenFileUrl(object dataitem)
    {
        if (dataitem != null)
        {
            FileInfo objFile = (FileInfo)dataitem;
            return BaseFileServerUrl + objFile.Path;          

        }
        return string.Empty;
    }
}